package cn.iocoder.yudao.module.alipay.controller.admin.drawingaccount;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.alipay.controller.admin.drawingaccount.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.drawingaccount.DrawingAccountDO;
import cn.iocoder.yudao.module.alipay.convert.drawingaccount.DrawingAccountConvert;
import cn.iocoder.yudao.module.alipay.service.drawingaccount.DrawingAccountService;

@Tag(name = "管理后台 - 收款账户")
@RestController
@RequestMapping("/alipay/drawing-account")
@Validated
public class DrawingAccountController {

    @Resource
    private DrawingAccountService drawingAccountService;

    @PostMapping("/create")
    @Operation(summary = "创建收款账户")
    @PreAuthorize("@ss.hasPermission('alipay:drawing-account:create')")
    public CommonResult<Long> createDrawingAccount(@Valid @RequestBody DrawingAccountCreateReqVO createReqVO) {
        return success(drawingAccountService.createDrawingAccount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新收款账户")
    @PreAuthorize("@ss.hasPermission('alipay:drawing-account:update')")
    public CommonResult<Boolean> updateDrawingAccount(@Valid @RequestBody DrawingAccountUpdateReqVO updateReqVO) {
        drawingAccountService.updateDrawingAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收款账户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('alipay:drawing-account:delete')")
    public CommonResult<Boolean> deleteDrawingAccount(@RequestParam("id") Long id) {
        drawingAccountService.deleteDrawingAccount(id);
        return success(true);
    }

    @GetMapping("/getByUserId")
    @Operation(summary = "获得收款账户根据userid")
    @PreAuthorize("@ss.hasPermission('alipay:drawing-account:query')")
    public CommonResult<DrawingAccountRespVO> getByUserId(@RequestParam("userId") String userId) {
        return success(drawingAccountService.getByUserId(userId));
    }

    @GetMapping("/list")
    @Operation(summary = "获得收款账户列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('alipay:drawing-account:query')")
    public CommonResult<List<DrawingAccountRespVO>> getDrawingAccountList(@RequestParam("ids") Collection<Long> ids) {
        List<DrawingAccountDO> list = drawingAccountService.getDrawingAccountList(ids);
        return success(DrawingAccountConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收款账户分页")
    @PreAuthorize("@ss.hasPermission('alipay:drawing-account:query')")
    public CommonResult<PageResult<DrawingAccountRespVO>> getDrawingAccountPage(@Valid DrawingAccountPageReqVO pageVO) {
        PageResult<DrawingAccountDO> pageResult = drawingAccountService.getDrawingAccountPage(pageVO);
        return success(DrawingAccountConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收款账户 Excel")
    @PreAuthorize("@ss.hasPermission('alipay:drawing-account:export')")
    @OperateLog(type = EXPORT)
    public void exportDrawingAccountExcel(@Valid DrawingAccountExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DrawingAccountDO> list = drawingAccountService.getDrawingAccountList(exportReqVO);
        // 导出 Excel
        List<DrawingAccountExcelVO> datas = DrawingAccountConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "收款账户.xls", "数据", DrawingAccountExcelVO.class, datas);
    }

}
